﻿namespace ClassRoom.Commands
{
    using System;
    using System.Windows.Input;

    public class DelegateCommand<T> : ICommand
    {
        #region Fields
        
        private readonly Action<T> executeMethod = null;
        private readonly Func<T, bool> canExecuteMethod = null;

        #endregion

        #region Constructors

        public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
        {            
            this.executeMethod = executeMethod;
            this.canExecuteMethod = canExecuteMethod;
        }

        public DelegateCommand(Action<T> executeMethod) : this(executeMethod, null) 
        { 
        }

        #endregion

        #region ICommand Members

        public event EventHandler CanExecuteChanged
        {
            add { }
            remove { }
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecuteMethod == null ? true : this.canExecuteMethod.Invoke((T)parameter);
        }

        public void Execute(object parameter)
        {
            if (this.executeMethod != null)
            {
                this.executeMethod.Invoke((T)parameter);
            }
        }

        #endregion
    }
}
